package com.kgc.basicprovider.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.pojo.Unit;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.basicprovider.dao.UnitMapper;
import com.kgc.basicprovider.service.UnitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
@Service
public class UnitServiceImpl implements UnitService {

    @Autowired
    private UnitMapper unitMapper;

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public ResponseMsg getUnitList(Map<String, Object> paramMap) {
        ResponseMsg msg = new ResponseMsg();
        logger.info("UnitServiceImpl getUnitList paramMap:"+paramMap);
        PageInfo<Unit> pageInfo = new PageInfo<>();
        if(paramMap.containsKey("pageNo")){
            pageInfo.setPageNo(Integer.valueOf(paramMap.get("pageNo").toString()));
        }
        if(paramMap.containsKey("pageSize")){
            pageInfo.setPageSize(Integer.valueOf(paramMap.get("pageSize").toString()));
        }
        Page<Object> page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<Unit> unitList = unitMapper.getUnitList(paramMap);
        logger.debug("UnitServiceImpl getUnitList unitList:"+unitList);
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal((int)page.getTotal());
        pageInfo.setList(unitList);
        logger.debug("UnitServiceImpl getUnitList pageInfo:"+pageInfo);
        msg.setCode("200");
        msg.setData(pageInfo);
        msg.setMsg("页面查询成功");
        return msg;
    }

    @Override
    public ResponseMsg getAllUnit() {
        ResponseMsg msg = new ResponseMsg();
        logger.info("UnitServiceImpl getAllUnit is start");
        PageInfo<Unit> pageInfo = new PageInfo<>();

        List<Unit> unitList = unitMapper.getAllUnit();
        logger.debug("UnitServiceImpl getAllUnit unitList:"+unitList);
        msg.setCode("200");
        msg.setData(unitList);
        msg.setMsg("页面查询成功");
        return msg;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public ResponseMsg getUnitById(Integer id) {
        ResponseMsg msg = new ResponseMsg();
        logger.info("UnitServiceImpl getUnitById id:"+id);
        Unit unit = unitMapper.getUnitById(id);
        logger.debug("UnitServiceImpl getUnitById unit:"+unit);
        if(unit==null){
            msg.setCode("201");
            msg.setMsg("没找到计量单位");
            msg.setData(unit);
        }else{
            msg.setCode("200");
            msg.setMsg("计量单位已找到");
            msg.setData(unit);
        }
        logger.debug("UnitServiceImpl getUnitById msg:"+msg);
        return msg;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public ResponseMsg getUnitByName(String name) {
        ResponseMsg msg = new ResponseMsg();
        logger.info("UnitServiceImpl getUnitByName name:"+name);
        Unit unit = unitMapper.getUnitByName(name);
        logger.debug("UnitServiceImpl getUnitByName unit:"+unit);
        if(unit==null){
            msg.setCode("201");
            msg.setMsg("没找到计量单位");
            msg.setData(unit);
        }else{
            msg.setCode("200");
            msg.setMsg("计量单位已找到");
            msg.setData(unit);
        }
        logger.debug("UnitServiceImpl getUnitByName msg:"+msg);
        return msg;
    }

    @Override
    public ResponseMsg addUnit(Unit unit) {
        ResponseMsg msg = new ResponseMsg();
        unit.setCreateDate(new Date());
        unit.setCreateUserId(1);
        unit.setUpdateDate(new Date());
        unit.setUpdateUserId(1);
        logger.info("UnitServiceImpl addUnit unit:"+unit);
        int insertCount = unitMapper.insertUnit(unit);
        logger.debug("UnitServiceImpl addUnit insertCount:"+insertCount);
        if(insertCount>0){
            msg.setCode("200");
            msg.setMsg("添加成功");
            msg.setData(true);
        }else{
            msg.setCode("201");
            msg.setMsg("添加失败");
            msg.setData(false);
        }
        logger.debug("UnitServiceImpl addUnit msg:"+msg);
        return msg;
    }

    @Override
    public ResponseMsg modifyUnit(Unit unit) {
        ResponseMsg msg = new ResponseMsg();
        unit.setCreateUserId(1);
        unit.setCreateDate(unit.getCreateDate());
        unit.setUpdateDate(new Date());
        unit.setUpdateUserId(1);
        logger.info("UnitServiceImpl modifyUnit unit:"+unit);
        int updateCount = unitMapper.updateUnit(unit);
        logger.debug("UnitServiceImpl modifyUnit updateCount:"+updateCount);
        if(updateCount>0){

            msg.setCode("200");
            msg.setMsg("修改成功");
            msg.setData(true);
        }else{
            msg.setCode("201");
            msg.setMsg("修改失败");
            msg.setData(false);
        }
        logger.debug("UnitServiceImpl modifyUnit msg:"+msg);
        return msg;
    }

    @Override
    public ResponseMsg delUnit(Integer id) {
        ResponseMsg msg = new ResponseMsg();
        logger.info("UnitServiceImpl delUnit id:"+id);
        int delCount = unitMapper.delUnit(id);
        logger.debug("UnitServiceImpl delUnit delCount:"+delCount);
        if(delCount>0){
            msg.setCode("200");
            msg.setMsg("删除成功");
            msg.setData(true);
        }else{
            msg.setCode("201");
            msg.setMsg("删除失败");
            msg.setData(false);
        }
        logger.debug("UnitServiceImpl delUnit msg:"+msg);
        return msg;
    }
}
